package com.yuejian.neusoft.yuejian_24.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.yuejian.neusoft.yuejian_24.R;
import com.yuejian.neusoft.yuejian_24.javaBean.Activity;
import com.yuejian.neusoft.yuejian_24.javaBean.Collected;
import com.yuejian.neusoft.yuejian_24.javaBean.Success;
import com.yuejian.neusoft.yuejian_24.listener.ActivityListener;
import com.yuejian.neusoft.yuejian_24.listener.CollectedListener;
import com.yuejian.neusoft.yuejian_24.listener.SuccessListener;
import com.yuejian.neusoft.yuejian_24.model.ActivityModel;

public class ActivityDetailActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView title, address, time, price, many, phone, introduction, signup;
    private ImageView img, back, collect;
    private Intent intent;
    private String act_id;
    private Context mContext;
    private SharedPreferences sp;

    ActivityListener activityListener = new ActivityListener() {
        @Override
        public void onResponse(Activity info) {
            title.setText(info.getAct_name());
            address.setText(info.getLocation());
            time.setText(info.getAct_time());
            price.setText(info.getMoney());
            many.setText(info.getPeoplenum());
            phone.setText(info.getAct_phone());
            introduction.setText("\u3000\u3000" + info.getAct_content());
            Picasso.with(mContext).load("http://10.0.2.2/yuejian/Public/photo/" + info.getAct_pic()).into(img);
        }

        @Override
        public void onFail() {

        }
    };
    CollectedListener collectedListener = new CollectedListener() {
        @Override
        public void onResponse(Collected collected) {
            if (collected.getCollected().equals("1")){
                collect.setImageResource(R.drawable.ic_collect1);
            }
            else if (collected.getCollected().equals("0")){
                collect.setImageResource(R.drawable.ic_collect);
            }
        }

        @Override
        public void onFail() {

        }
    };
     SuccessListener successListener = new SuccessListener() {
        @Override
        public void onResponse(Success success) {
            if (success.getSuccess().equals("1")){
                Toast.makeText(ActivityDetailActivity.this,"关注成功", Toast.LENGTH_SHORT).show();
                collect.setImageResource(R.drawable.ic_collect1);
            }else if (success.getSuccess().equals("0")){
                Toast.makeText(ActivityDetailActivity.this,"取消关注成功", Toast.LENGTH_SHORT).show();
                collect.setImageResource(R.drawable.ic_collect);
            }
        }

        @Override
        public void onFail() {

        }
    };


    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_detail);
        initViews();
        initEvent();
        act_id = getIntent().getStringExtra("act_id").toString();
        ActivityModel activityModel = new ActivityModel();
        activityModel.getacdetail(act_id, activityListener);
        activityModel.isCollected(act_id,sp.getString("uid",""),"1",collectedListener);

    }

    private void initEvent() {
        back.setOnClickListener(this);
        collect.setOnClickListener(this);
        signup.setOnClickListener(this);
    }






    private void initViews() {
        title = (TextView) findViewById(R.id.ac_detail_title);
        address = (TextView) findViewById(R.id.ac_detail_address);
        time = (TextView) findViewById(R.id.ac_detail_time);
        price = (TextView) findViewById(R.id.ac_detail_price);
        many = (TextView) findViewById(R.id.ac_detail_many);
        phone = (TextView) findViewById(R.id.ac_detail_phone);
        introduction = (TextView) findViewById(R.id.ac_detail_introduction);
        signup = (TextView) findViewById(R.id.ac_detail_signup);
        img = (ImageView) findViewById(R.id.ac_detail_img);
        back = (ImageView) findViewById(R.id.ac_detail_back);
        collect = (ImageView) findViewById(R.id.ac_detail_dian);
        sp = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ac_detail_back:
                finish();
                break;

            case R.id.ac_detail_dian:
                ActivityModel activityModel = new ActivityModel();
                activityModel.follow(getIntent().getStringExtra("act_id"),sp.getString("uid",""),"1",successListener);
                break;
            case R.id.ac_detail_signup:
                intent = new Intent(ActivityDetailActivity.this,ActivityJoinActivity.class);
                intent.putExtra("act_id",act_id);
                intent.putExtra("uid",sp.getString("uid",""));
                startActivity(intent);
                finish();
                break;
        }
        }
    }

